home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / game / board / Crafty-15.19.lha / crafty-15.19 / src / validate.c < prev    next >
C/C++ Source or Header  |  1998-09-13  |  17KB  |  572 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "chess.h"
  4. #include "data.h"
  5.  
  6. void ValidatePosition(TREE *tree, int ply, int move, char *caller)
  7. {
  8.   BITBOARD temp, temp_occ, temp_occ_rl90, temp_occ_rl45;
  9.   BITBOARD temp_occ_rr45, temp_occx, cattacks, rattacks;
  10.   unsigned int temp1;
  11.   int i,square,error;
  12.   int temp_score;
  13. /*
  14.   first, test w_occupied and b_occupied
  15. */
  16.   error=0;
  17.   temp_occ=Or(Or(Or(Or(Or(WhitePawns,WhiteKnights),WhiteBishops),
  18.                     WhiteRooks),WhiteQueens),WhiteKing);
  19.   if(Xor(WhitePieces,temp_occ)) {
  20.     Print(128,"ERROR white occupied squares is bad!\n");
  21.     Display2BitBoards(temp_occ,WhitePieces);
  22.     error=1;
  23.   }
  24.   temp_occ=Or(Or(Or(Or(Or(BlackPawns,BlackKnights),BlackBishops),
  25.                     BlackRooks),BlackQueens),BlackKing);
  26.   if(Xor(BlackPieces,temp_occ)) {
  27.     Print(128,"ERROR black occupied squares is bad!\n");
  28.     Display2BitBoards(temp_occ,BlackPieces);
  29.     error=1;
  30.   }
  31. /*
  32.   now test rotated occupied bitboards.
  33. */
  34.   temp_occ_rl90=0;
  35.   temp_occ_rl45=0;
  36.   temp_occ_rr45=0;
  37.   for (i=0;i<64;i++) {
  38.     if (PieceOnSquare(i)) {
  39.       temp_occ_rl90=Or(temp_occ_rl90,set_mask_rl90[i]);
  40.       temp_occ_rl45=Or(temp_occ_rl45,set_mask_rl45[i]);
  41.       temp_occ_rr45=Or(temp_occ_rr45,set_mask_rr45[i]);
  42.     }
  43.   }
  44.   if(Xor(OccupiedRL90,temp_occ_rl90)) {
  45.     Print(128,"ERROR occupied squares (rotated left 90) is bad!\n");
  46.     Display2BitBoards(temp_occ_rl90,OccupiedRL90);
  47.     error=1;
  48.   }
  49.   if(Xor(OccupiedRL45,temp_occ_rl45)) {
  50.     Print(128,"ERROR occupied squares (rotated left 45) is bad!\n");
  51.     Display2BitBoards(temp_occ_rl45,OccupiedRL45);
  52.     error=1;
  53.   }
  54.   if(Xor(OccupiedRR45,temp_occ_rr45)) {
  55.     Print(128,"ERROR occupied squares (rotated right 45) is bad!\n");
  56.     Display2BitBoards(temp_occ_rr45,OccupiedRR45);
  57.     error=1;
  58.   }
  59. /*
  60.   now test bishops_queens and rooks_queens
  61. */
  62.   temp_occ=Or(Or(Or(WhiteBishops,WhiteQueens),BlackBishops),
  63.               BlackQueens);
  64.   if(Xor(BishopsQueens,temp_occ)) {
  65.     Print(128,"ERROR bishops_queens is bad!\n");
  66.     Display2BitBoards(temp_occ,BishopsQueens);
  67.     error=1;
  68.   }
  69.     temp_occ=Or(Or(Or(WhiteRooks,WhiteQueens),BlackRooks),
  70.                 BlackQueens);
  71.   if(Xor(RooksQueens,temp_occ)) {
  72.     Print(128,"ERROR rooks_queens is bad!\n");
  73.     Display2BitBoards(temp_occ,RooksQueens);
  74.     error=1;
  75.   }
  76. /*
  77.   check individual piece bit-boards to make sure two pieces
  78.   don't occupy the same square (bit)
  79. */
  80.     temp_occ=Xor(Xor(Xor(Xor(Xor(Xor(Xor(Xor(Xor(Xor(Xor(
  81.        WhitePawns,WhiteKnights),WhiteBishops),WhiteRooks),
  82.        WhiteQueens),BlackPawns),BlackKnights),BlackBishops),
  83.        BlackRooks),BlackQueens),WhiteKing),BlackKing);
  84.     temp_occx=Or(Or(Or(Or(Or(Or(Or(Or(Or(Or(Or(
  85.        WhitePawns,WhiteKnights),WhiteBishops),WhiteRooks),
  86.        WhiteQueens),BlackPawns),BlackKnights),BlackBishops),
  87.        BlackRooks),BlackQueens),WhiteKing),BlackKing);
  88.     if(Xor(temp_occ,temp_occx)) {
  89.       Print(128,"ERROR two pieces on same square\n");
  90.       error=1;
  91.     }
  92. /*
  93.   test material_evaluation
  94. */
  95.   temp_score=PopCnt(WhitePawns)*PAWN_VALUE;
  96.   temp_score-=PopCnt(BlackPawns)*PAWN_VALUE;
  97.   temp_score+=PopCnt(WhiteKnights)*KNIGHT_VALUE;
  98.   temp_score-=PopCnt(BlackKnights)*KNIGHT_VALUE;
  99.   temp_score+=PopCnt(WhiteBishops)*BISHOP_VALUE;
  100.   temp_score-=PopCnt(BlackBishops)*BISHOP_VALUE;
  101.   temp_score+=PopCnt(WhiteRooks)*ROOK_VALUE;
  102.   temp_score-=PopCnt(BlackRooks)*ROOK_VALUE;
  103.   temp_score+=PopCnt(WhiteQueens)*QUEEN_VALUE;
  104.   temp_score-=PopCnt(BlackQueens)*QUEEN_VALUE;
  105.   if(temp_score != Material) {
  106.     Print(128,"ERROR  material_evaluation is wrong, good=%d, bad=%d\n",
  107.            temp_score,Material);
  108.     error=1;
  109.   }
  110.   temp_score=PopCnt(WhiteKnights)*knight_v;
  111.   temp_score+=PopCnt(WhiteBishops)*bishop_v;
  112.   temp_score+=PopCnt(WhiteRooks)*rook_v;
  113.   temp_score+=PopCnt(WhiteQueens)*queen_v;
  114.   if(temp_score != TotalWhitePieces) {
  115.     Print(128,"ERROR  white_pieces is wrong, good=%d, bad=%d\n",
  116.            temp_score,TotalWhitePieces);
  117.     error=1;
  118.   }
  119.   temp_score=PopCnt(WhiteKnights);
  120.   temp_score+=PopCnt(WhiteBishops);
  121.   if(temp_score != WhiteMinors) {
  122.     Print(128,"ERROR  white_minors is wrong, good=%d, bad=%d\n",
  123.            temp_score,WhiteMinors);
  124.     error=1;
  125.   }
  126.   temp_score=PopCnt(WhiteRooks);
  127.   temp_score+=PopCnt(WhiteQueens)*2;
  128.   if(temp_score != WhiteMajors) {
  129.     Print(128,"ERROR  white_majors is wrong, good=%d, bad=%d\n",
  130.            temp_score,WhiteMajors);
  131.     error=1;
  132.   }
  133.   temp_score=PopCnt(WhitePawns);
  134.   if(temp_score != TotalWhitePawns) {
  135.     Print(128,"ERROR  white_pawns is wrong, good=%d, bad=%d\n",
  136.            temp_score,TotalWhitePawns);
  137.     error=1;
  138.   }
  139.   temp_score=PopCnt(BlackKnights)*knight_v;
  140.   temp_score+=PopCnt(BlackBishops)*bishop_v;
  141.   temp_score+=PopCnt(BlackRooks)*rook_v;
  142.   temp_score+=PopCnt(BlackQueens)*queen_v;
  143.   if(temp_score != TotalBlackPieces) {
  144.     Print(128,"ERROR  black_pieces is wrong, good=%d, bad=%d\n",
  145.            temp_score,TotalBlackPieces);
  146.     error=1;
  147.   }
  148.   temp_score=PopCnt(BlackKnights);
  149.   temp_score+=PopCnt(BlackBishops);
  150.   if(temp_score != BlackMinors) {
  151.     Print(128,"ERROR  black_minors is wrong, good=%d, bad=%d\n",
  152.            temp_score,BlackMinors);
  153.     error=1;
  154.   }
  155.   temp_score=PopCnt(BlackRooks);
  156.   temp_score+=PopCnt(BlackQueens)*2;
  157.   if(temp_score != BlackMajors) {
  158.     Print(128,"ERROR  black_majors is wrong, good=%d, bad=%d\n",
  159.            temp_score,BlackMajors);
  160.     error=1;
  161.   }
  162.   temp_score=PopCnt(BlackPawns);
  163.   if(temp_score != TotalBlackPawns) {
  164.     Print(128,"ERROR  black_pawns is wrong, good=%d, bad=%d\n",
  165.            temp_score,TotalBlackPawns);
  166.     error=1;
  167.   }
  168. /*
  169.   now test the board[...] to make sure piece values are correct.
  170. */
  171. /*
  172.    test pawn locations
  173. */
  174.   temp=WhitePawns;
  175.   while(temp) {
  176.     square=FirstOne(temp);
  177.     if (PieceOnSquare(square) != pawn) {
  178.       Print(128,"ERROR!  board[%d]=%d, should be 1\n",square,
  179.             PieceOnSquare(square));
  180.       error=1;
  181.     }
  182.     Clear(square,temp);
  183.   }
  184.   temp=BlackPawns;
  185.   while(temp) {
  186.     square=FirstOne(temp);
  187.     if (PieceOnSquare(square) != -pawn) {
  188.       Print(128,"ERROR!  board[%d]=%d, should be -1\n",square,
  189.             PieceOnSquare(square));
  190.       error=1;
  191.     }
  192.     Clear(square,temp);
  193.   }
  194. /*
  195.    test knight locations
  196. */
  197.   temp=WhiteKnights;
  198.   while(temp) {
  199.     square=FirstOne(temp);
  200.     if (PieceOnSquare(square) != knight) {
  201.       Print(128,"ERROR!  board[%d]=%d, should be 2\n",square,
  202.             PieceOnSquare(square));
  203.       error=1;
  204.     }
  205.     Clear(square,temp);
  206.   }
  207.   temp=BlackKnights;
  208.   while(temp) {
  209.     square=FirstOne(temp);
  210.     if (PieceOnSquare(square) != -knight) {
  211.       Print(128,"ERROR!  board[%d]=%d, should be -2\n",square,
  212.             PieceOnSquare(square));
  213.       error=1;
  214.     }
  215.     Clear(square,temp);
  216.   }
  217. /*
  218.    test bishop locations
  219. */
  220.   temp=WhiteBishops;
  221.   while(temp) {
  222.     square=FirstOne(temp);
  223.     if (PieceOnSquare(square) != bishop) {
  224.       Print(128,"ERROR!  board[%d]=%d, should be 3\n",square,
  225.             PieceOnSquare(square));
  226.       error=1;
  227.     }
  228.     rattacks=AttacksBishop(square);
  229.     cattacks=ValidateComputeBishopAttacks(tree,square);
  230.     if (rattacks != cattacks) {
  231.       Print(128,"ERROR!  bishop attacks wrong, square=%d\n",square);
  232.       Display2BitBoards(cattacks,rattacks);
  233.       error=1;
  234.     }
  235.     Clear(square,temp);
  236.   }
  237.   temp=BlackBishops;
  238.   while(temp) {
  239.     square=FirstOne(temp);
  240.     if (PieceOnSquare(square) != -bishop) {
  241.       Print(128,"ERROR!  board[%d]=%d, should be -3\n",square,
  242.             PieceOnSquare(square));
  243.       error=1;
  244.     }
  245.     rattacks=AttacksBishop(square);
  246.     cattacks=ValidateComputeBishopAttacks(tree,square);
  247.     if (rattacks != cattacks) {
  248.       Print(128,"ERROR!  bishop attacks wrong, square=%d\n",square);
  249.       Display2BitBoards(cattacks,rattacks);
  250.       error=1;
  251.     }
  252.     Clear(square,temp);
  253.   }
  254. /*
  255.    test rook locations
  256. */
  257.   temp=WhiteRooks;
  258.   while(temp) {
  259.     square=FirstOne(temp);
  260.     if (PieceOnSquare(square) != rook) {
  261.       Print(128,"ERROR!  board[%d]=%d, should be 4\n",square,
  262.             PieceOnSquare(square));
  263.       error=1;
  264.     }
  265.     rattacks=AttacksRook(square);
  266.     cattacks=ValidateComputeRookAttacks(tree,square);
  267.     if (rattacks != cattacks) {
  268.       Print(128,"ERROR!  Rook attacks wro